#!/usr/bin/env bash
### BEGIN INIT INFO
# Provides:          telemetry-server
# Required-Start:    networking
# Required-Stop:
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Start telemetry server service
# Description:       Wrapper for starting / stopping a telemetry http local server
### END INIT INFO
# SPDX-License-Identifier: BSD-3-Clause
#
# Copyright 2022 NXP

SERVER_PATH=/home/root/telemetry-server/telemetry_server.py
LOG_PATH=/var/log/telemetry-server-log.txt

case "$1" in
start)
        echo "Starting the telemetry server service"
        nohup ${SERVER_PATH} > ${LOG_PATH} 2>&1 &
        ;;
stop)
        echo "Stopping the telemetry server service"
        kill -9 $(ps aux | grep '[t]elemetry_server.py' | awk '{print $2}') 2> /dev/null || true
        ;;
restart)
        echo "Restarting the telemetry server service"
        kill -9 $(ps aux | grep '[t]elemetry_server.py' | awk '{print $2}') 2> /dev/null || true
        nohup ${SERVER_PATH} > ${LOG_PATH} 2>&1 &
        ;;
*)
        echo "Usage: /etc/init.d/telemetry-server {start|stop|restart}"
        exit 1
        ;;
esac

exit 0